/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import id.duapuluhdua.dakidaki.common.CommonActivity;

/**
 * The application main user interface.
 */
public class MainView extends SurfaceView implements SurfaceHolder.Callback, OnGestureListener
{
  /** Constants */
  private static final String TAG = "MainView";

  /** Fields */
  private MainThread mThread;
  private GestureDetector mGestureDetector;

  /**
   * Default constructor.
   *
   * @param context the application context.
   * @param attrs the view attributes.
   */
  public MainView(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    setFocusable(true);
    mGestureDetector = new GestureDetector(context, this);
    mGestureDetector.setIsLongpressEnabled(false);
  }

  /**
   * Initializes the thread.
   *
   * @param parent the application activity.
   */
  public void init(CommonActivity parent)
  {
    // Get the surface events
    SurfaceHolder holder = getHolder();
    holder.setType(SurfaceHolder.SURFACE_TYPE_HARDWARE);
    holder.addCallback(this);

    setFocusable(true);

    // Create the thread, it is started in surfaceCreated()
    mThread = new MainThread(holder, parent, new Handler()
    {
      @Override
      public void handleMessage(Message msg)
      {
        Log.i(TAG, msg.toString());
      }
    });
  }

  /**
   * Gets the animation thread.
   *
   * @return the animation thread.
   */
  public MainThread getThread()
  {
    return mThread;
  }

  @Override
  public boolean onTouchEvent(MotionEvent e)
  {
    boolean bHandled = false;

    if (mGestureDetector.onTouchEvent(e)) {
      bHandled = true;
    }
    if (e.getAction() == MotionEvent.ACTION_UP) {
      bHandled &= mThread.onActionUpEvent(e);
    }
    return bHandled;
  }

  // SurfaceHolder.Callback methods
  //
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height)
  {
    mThread.setSurfaceSize(width, height);
  }

  public void surfaceCreated(SurfaceHolder holder)
  {
    mThread.setStateRunning();
  }

  public void surfaceDestroyed(SurfaceHolder holder)
  {
    mThread.setStatePaused();
  }

  // OnGestureListener methods
  //
  public boolean onDown(MotionEvent e)
  {
    return mThread.onDownEvent(e);
  }

  public void onShowPress(MotionEvent e)
  {
    // Do nothing.
  }

  public boolean onSingleTapUp(MotionEvent e)
  {
    return true;
  }

  public boolean onScroll(MotionEvent e1, MotionEvent e2, float dx, float dy)
  {
    return mThread.onScrollEvent(dx, dy);
  }

  public void onLongPress(MotionEvent e)
  {
    // Do nothing.
  }

  public boolean onFling(MotionEvent e1, MotionEvent e2, float vx, float vy)
  {
    return false;
  }
}
